<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @clear="clear"
      @handleQuery="getList(1)"
    >
      <template v-if="meta.indexOf('TicketOrderVenueListExcel') > -1" #btns>
        <el-button
          :loading="exportLoading"
          type="primary"
          @click="batchexport"
        >{{ $t('message.daochu') }}</el-button>
      </template>
    </QueryForm>
    <div v-if="tjList && tjList.length > 0" class="total_wrap">
      <div class="first item">
        <div class="key">{{ $t('message.shanxuan') }}</div>
        <div class="key">{{ $t('message.heji') }}</div>
      </div>
      <div class="item_list">
        <div v-for="(item, i) in tjList" :key="i" class="item">
          <div class="key">{{ item.payMethod }}</div>
          <div class="value">
            <!-- <span>{{ $t('message.rmbfh') }}</span> -->
            <span class="price">{{ item.money }}</span>
          </div>
        </div>
      </div>
      <el-tooltip effect="dark" placement="left">
        <div class="jesm">
          <img
            style="width: 15px; margin-right: 2px"
            src="@/assets/icon/question.png"
            alt=""
          >{{ $t('message.jesm') }}
        </div>
        <div slot="content" v-html="smTip" />
      </el-tooltip>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="$t('message.mpph')"
        prop="id"
        min-width="210"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.mpmc')"
        prop="ticketName"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.mpsj')"
        prop="price"
        min-width="110"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.sfk')"
        prop="realPrice"
        min-width="110"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.ytk')"
        prop="refundPrice"
        min-width="110"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.ddh')"
        prop="orderId"
        min-width="220"
      />
      <el-table-column
        align="center"
        :label="$t('message.ddly')"
        min-width="110"
        prop="channelTypeName"
      />
      <el-table-column
        prop="venueName"
        align="center"
        :label="$t('message.cgmc')"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.zffs')"
        prop="payMethod"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.memberInfo')"
        min-width="240"
        show-overflow-tooltip
        prop="userInfo"
      />
      <el-table-column
        align="center"
        :label="$t('message.createTime')"
        prop="createTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        :label="$t('message.czy')"
        prop="creatorName"
        min-width="120"
        show-overflow-tooltip
      />
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import {
  ticketOrdersb,
  configList,
  ticketOrderVenueListExcel
} from '@/api'
import { parseTime } from '@/utils'
import dayjs from 'dayjs'
import { queryFormConfig } from './config'
import Pagination from '@/components/paginations'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      meta: [],
      queryFormConfig,
      venueList: [],
      list: [],
      total: 0,
      pagination: {
        page: 1,
        rows: 10
      },
      listLoading: false,
      exportLoading: false,
      payTypes: [],
      tjList: [],
      filters: {},
      smTip: ''
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.init()
    this.getInitData()
    // this.getChannel()
  },
  methods: {
    // getChannel() {
    //   channelConfigPost({ param: { code: 'CHANNEL_CARD' }}).then(res => {
    //     this.queryFormConfig.formItems[5].options = res.recordList || []
    //   })
    // },
    init() {
      let time = []
      const start = new Date()
      const end = new Date()
      start.setTime(end.getTime() - 3600 * 1000 * 24 * 29)
      time = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
      this.$set(this.filters, 'time', time)
      this.getList(1)
    },
    nameWithPayMethod(payM) {
      const temp = this.payTypes.find((item) => item.value === payM)
      return temp ? temp.name : '-'
    },
    getInitData() {
      configList({ param: { type: '2', code: '' }})
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.payTypes = res.recordList
          const arr = res.recordList
          this.queryFormConfig.formItems[5].options = arr
        })
        .catch(() => { })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (filters.time && filters.time.length > 0) {
        filters.startDate = filters.time[0]
        filters.endDate = filters.time[1]
      } else {
        filters.startDate = ''
        filters.endDate = ''
      }
      filters.venueId = sessionStorage.getItem('venueId')
      pagination.firstQueryTime = parseTime(new Date())
      ticketOrdersb({
        pagination,
        param: {
          ...filters
        }
      }).then((res) => {
        this.listLoading = false
        if (res.errorCode !== '000000') return
        this.list = res.recordList
        this.total = res.totalCount
        const tjList = res.data
        if (tjList) {
          let smTip = ''
          tjList.forEach(s => {
            smTip = smTip + s.tip + '<br />'
          })
          this.tjList = tjList
          this.smTip = smTip
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    clear() {
      this.filters = {
        time: this.filters.time
      }
      this.getList(1)
    },
    batchexport() {
      const { filters } = this
      if (filters.time && filters.time.length > 0) {
        filters.startDate = filters.time[0]
        filters.endDate = filters.time[1]
      } else {
        filters.startDate = ''
        filters.endDate = ''
      }
      filters.venueId = sessionStorage.getItem('venueId')
      this.exportLoading = true
      ticketOrderVenueListExcel({ param: { ...filters }}).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }, () => {
        this.exportLoading = false
      })
    }
  }
}
</script>
